#include <openssl/evp.h>
#include <stdio.h>
#include <string.h>

int sm3_hash(const unsigned char *data, size_t len, unsigned char *out) {
    EVP_MD_CTX *ctx = EVP_MD_CTX_new();
    if (!ctx) return -1;

    if (EVP_DigestInit_ex(ctx, EVP_sm3(), NULL) != 1) goto err;
    if (EVP_DigestUpdate(ctx, data, len) != 1) goto err;
    if (EVP_DigestFinal_ex(ctx, out, NULL) != 1) goto err;

    EVP_MD_CTX_free(ctx);
    return 0;

err:
    EVP_MD_CTX_free(ctx);
    return -1;
}

int main() {
    const char *msg = "20231318fsl";
    unsigned char hash[32]; // SM3 输出 256 位 = 32 字节
    printf("SM3-HASH:");
    if (sm3_hash((unsigned char*)msg, strlen(msg), hash) == 0) {
        for (int i = 0; i < 32; i++) printf("%02x", hash[i]);
	printf("\n");
    }
    return 0;
}
